<?php
require_once(APPPATH.'models/Basemodel.php');
class Userplacemodel extends BaseModel
{
    private static $table = "gm_user_place";
    
    private static $cols = "id,uid,pid,c_time,u_id";

    function __construct()
    {
        parent::__construct();
    }

    function  countAll(){
        return $this->db->from(self::$table)->count_all_results();
    }

    function listmap(){
        $array=$this->db->get(self::$table)->result_array();
        $map=array();
        foreach ($array as $user) {
            $map[$user['id']]=$user;
        }
        return $map;
    }

    function  getByPid($pid){
        $this->db->where("pid",$pid);
        return $this->db->get(self::$table)->row_array();
    }



    function  getById($id){
        $this->db->where("id", $id);
        return $this->db->get(self::$table)->row_array();
    }

    function getByUid($uid) {
        $this->db->where('uid', $uid);
        return $this->db->get(self::$table)->result_array();
    }

     function getByU_Id($u_id) {
        $this->db->where('u_id', $u_id);
        return $this->db->get(self::$table)->result_array();
    }

    function insert($data)
    {
        $result=$this->db->insert(self::$table,$data);
        return $result;
    }

    function insertBatch($data)
    {
        $result=$this->db->insert_batch(self::$table,$data);
        return $result;
    }

    function update($data, $id)
    {
        $this->db->where("id", $id);
        $result=$this->db->update(self::$table,$data);
        return $result;
    }

    function updateBatch($data,$where)
    {
        foreach ($data as $item) {
            $this->db->where("pid", $item['pid']);
            $result=$this->db->update(self::$table,array('uid' => $item['uid'], 'c_time' => $item['c_time']));
            if (!$result) {
                return $result;
            }
        }

        return true;
    }

    function delete($id)
    {
        $this->db->where("id",$id);
        $result=$this->db->delete(self::$table);
        return $result;
    }

    function  lists(){
        $list=$this->db->get(self::$table)->result_array();
        $map=array();
        foreach($list as $l){
            $map[$l['key']]=$l['value'];
        }
        return $map;
    }

    function countByCondition($where)
    {
        $sql="select count(1) as num from ".self::$table." where 1=1  ".$where;
        $row=$this->db->query($sql)->row_array();
        return $row['num'];
    }



}
